import "package:flutter/material.dart";
import 'package:flutter_pink_app/composables/video_row.dart';
import 'package:flutter_pink_app/composables/video_toolbar.dart';
import 'package:flutter_pink_app/serialized/recommend.dart';
import 'package:flutter_pink_app/storage/video_page_provider.dart';
import 'package:flutter_pink_app/style/text.dart';
import "appbar_tabbar.dart";
import "detail_info.dart";
import '../style/color.dart';
import "package:flutter_pink_app/util.dart";

typedef void HandleIconCallback(Icon icon);

// 视频详情页组合
class VideoDetail extends StatelessWidget {
  final List<String> _tabs; // tabs页
  final VideoInfo _videoInfo; // 影片信息
  final HandleIconCallback handleIconTap; // 处理右侧图标点击事件函数
  final Function handleFocusTap; // 处理关注按钮函数
  final Icon rightIcon; // 右侧图标
  final double shadowElevation; // 阴影高度

  const VideoDetail(this._tabs, this._videoInfo,
      {Key key,
      this.handleIconTap,
      this.handleFocusTap,
      this.shadowElevation = 8,
      this.rightIcon = const Icon(Icons.live_tv_sharp, color: mainThemeColor)})
      : super(key: key);

  // 构建详情页tab方法
  Material _buildDetailTab() {
    return Material(
      shadowColor: lightGrayTextColor,
      elevation: shadowElevation,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          buildAppbarTabbar(
              strList: _tabs,
              selectedStyle:
                  TextStyle(fontSize: 16, fontWeight: FontWeight.w700),
              unselectedStyle: TextStyle(fontSize: 16),
              tabMargin: EdgeInsets.fromLTRB(30, 5, 30, 5)),
          Padding(
            padding: const EdgeInsets.only(right: 20),
            child: GestureDetector(
              onTap: () => handleIconTap(rightIcon),
              child: rightIcon,
            ),
          )
        ],
      ),
    );
  }

  // 构建详情作者栏方法
  Padding _buildDetailAuthorBar() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 15),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[_buildAvatar(_videoInfo.owner), _buildFocusButton()],
      ),
    );
  }

  // 构建详情作者头像方法
  Row _buildAvatar(Owner author) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        ClipRRect(
          borderRadius: BorderRadius.circular(20),
          child: Image.network(author.face, width: 40, height: 40),
        ),
        Padding(
          padding: EdgeInsets.only(left: 10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(bottom: 3),
                child: Text(author.name, style: mainThemeStyle),
              ),
              Text(
                  "${formatFansCount(author.fans)}粉丝  ${formatVideoCount(author.fans)}视频",
                  style: videoCardStyle)
            ],
          ),
        )
      ],
    );
  }

  // 构建关注按钮函数
  MaterialButton _buildFocusButton() {
    return MaterialButton(
      onPressed: handleFocusTap ?? () => print('点击了关注'),
      highlightElevation: 0,
      padding: EdgeInsets.symmetric(horizontal: 15, vertical: 5),
      minWidth: 50,
      height: 13,
      color: mainThemeColor,
      child: Center(child: Text('+ 关注', style: focusWhiteStyle)),
    );
  }

  // 构建简介栏函数
  ListView _buildIntro(BuildContext ctx, {Key key}) {
    final provider = VideoPageProvider.of(ctx); // 取得数据提供者
    final List<VideoInfo> relatedVideoList = provider.detail?.videoList ?? [];
    return ListView(
      key: key,
      physics: BouncingScrollPhysics(),
      children: <Widget>[
        _buildDetailAuthorBar(),
        VideoInfoPanel(_videoInfo, key: key),
        VideoDetailToolbar(
          ctx,
          key: ValueKey('Toolbar'),
          thumbCount: _videoInfo.like,
          coinCount: _videoInfo.coin,
          recordCount: _videoInfo.favorite,
          shareCount: _videoInfo.share,
        ),
        for (final video in relatedVideoList) VideoRow(video)
      ],
    );
  }

  // 构建评论栏函数
  Center _buildRemark() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset("images/2233_404.png", height: 178, fit: BoxFit.cover),
          Text("什么都没找到啊T T", style: normalGrayStyle)
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext detailContext) {
    return DefaultTabController(
      length: _tabs.length ?? 0,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _buildDetailTab(),
          Expanded(
            child: TabBarView(
              physics: BouncingScrollPhysics(),
              children: <Widget>[
                _buildIntro(detailContext, key: ValueKey('intro')),
                _buildRemark()
              ],
            ),
          )
        ],
      ),
    );
  }
}
